package sg.edu.nus.iss.team8s.vmcs.builder;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 
 * @author Zheng Xiaofeng
 *
 */
public class BuilderFactory {
	
	private static BuilderFactory factory = null;
	private ApplicationContext context;
	
	private BuilderFactory(){
		context = new ClassPathXmlApplicationContext("classpath:sg/edu/nus/iss/team8s/vmcs/builder/UIBuilder.xml");
	}
	
	public static synchronized BuilderFactory getInstance(){
		if(factory == null){
			factory = new BuilderFactory();
		}
		return factory;
	}
	
	public UIBuilder getUIBuilder(String uiBuilderName){
		UIBuilder uiBuilder = (UIBuilder)context.getBean(uiBuilderName);
		if(uiBuilder == null){
			throw new RuntimeException("Can not find the UIBuilder for " + uiBuilderName);
		}
		return uiBuilder;
	}
	
	public static void main(String[] args){
		UIBuilder builder1 = BuilderFactory.getInstance().getUIBuilder("customerUIBuilder");
		System.out.println(builder1.getClass().getName());
		
		UIBuilder builder2 = BuilderFactory.getInstance().getUIBuilder("machineryUIBuilder");
		System.out.println(builder2.getClass().getName());
		
		UIBuilder builder3 = BuilderFactory.getInstance().getUIBuilder("maintenanceUIBuilder");
		System.out.println(builder3.getClass().getName());
	}
}
